var processor = {
  timeoutId: null,
  handle: function () {
    console.log('window.innerWidth ', window.innerWidth)
  },
  process: function () {
    clearTimeout(this.timeoutId)
    var that = this
    this.timeoutId = setTimeout(function () {
      that.handle()
    }, 100)
  }
}
window.onresize = function () {
  processor.process()
}

// 实现一个观察者模式
function EventTarget () {
  this.handlers = {}
}
EventTarget.prototype = {
  constructor: EventTarget,
  addHandler: function (type, handler) {
    if (typeof this.handlers[type] == 'undefined') {
      this.handlers[type] = []
    }
    this.handlers[type].push(handler)
  },
  fire: function (event) {
    if (!event.target) {
      event.target = this
    }
    if (this.handlers[event.type] instanceof Array) {
      var handlers = this.handlers[event.type]
      for (var i = 0, len = handlers.length; i < len; i++) {
        handlers[i](event)
      }
    }
  },
  removeHandler: function (type, handler) {
    if (this.handlers[type] instanceof Array) {
      var handlers = this.handlers[type]
      for (var i = 0, len = handlers.length; i < len; i++) {
        if (handlers[i] === handler) {
          break
        }
      }
      handlers.splice(i, 1)
    }
  }
}

// 事件处理程序
function handleMessage (event) {
  console.log("Message received: " + event.message)
}
// 创建一个新对象
var target = new EventTarget()
// 添加一个事件处理程序
target.addHandler('message', handleMessage)
// 触发事件
target.fire({type: 'message', message: 'Hello world'})
